package cn.bgs.controller;

import cn.bgs.pojo.*;
import cn.bgs.service.EmpService;
import cn.bgs.service.StuService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/emp")
public class EmpController {

    Logger log = LogManager.getLogger();

    @RequestMapping("/text")
    public String text(){
        return "/view/text";
    }

    @RequestMapping("/test")
    public String test(){
        return "test02";
    }

    @RequestMapping("/test22")
    @ResponseBody
    public Map<String,Object> test22(@RequestBody Test test){
        Map<String,Object> abc = new HashMap<>();
        log.info("Test:::"+test);
        abc.put("name","0000");
        abc.put("email","测试成功");
        return abc;
    }

    @RequestMapping("/test33")
    @ResponseBody
    public Map<String,Object> test33(@RequestBody String data){
        Map<String,Object> map = new HashMap<>();
        log.info("str◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆"+data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONObject jsonObject1 = jsonObject.getJSONObject("response");
        JSONObject data1 = jsonObject1.getJSONObject("data");
        JSONArray cnameRecords = data1.getJSONArray("cnameRecords");

        for (int i = 0; i < cnameRecords.size(); i++) {
            JSONObject bb = cnameRecords.getJSONObject(i);
            if ("webmail.a.co.uk.".equals(bb.getString("aliasHost"))){
                        log.info("aliasHost:::◆◆◆◆◆◆◆◆◆◆◆◆◆"+bb.getString("aliasHost"));
                        log.info("canonicalHost:::◆◆◆◆◆◆◆◆◆◆◆◆◆"+bb.getString("canonicalHost"));
                    }
                }
        return map;
    }






    @Autowired
    private EmpService empService;

    @Autowired
    private StuService stuService;


    //跳转登录界面
    @RequestMapping("/a")
    public  String a(){
        return "/view/login";
    }
    //跳转注册界面
    @RequestMapping("/b")
    public  String b(){
        return "/view/register";
    }

    //登录
    @RequestMapping("/denglu")
    //判断登录是教师或学生添加形参'role'  自动映射
    public String denglu(String email, String password, Model mo, Integer role, HttpSession session){
        log.info("登录需要的账号和密码");
        EmployeeRegister denglu = empService.denglu(email, password);

        StudentRegister student = empService.stuLogin(email, password);
        if(denglu!=null && role==1){

            return "/view/stushow";
        }else if (student!=null && role==2){
            Student stu = stuService.selStuInfo(student.getStuNum());

            //session域获取到student表信息传入页面
            session.setAttribute("stu",stu);

            return "/view/perInformation";
        }
        mo.addAttribute("error","用户名或密码错误");
        return "/view/login";
    }


    //注册
    @RequestMapping("/zhuce")
    public String zhuce(Vo vo){
        int a = empService.zhuce(vo);
        int b = empService.zhuce2(vo);
        if (a>0 && b>0){
            return "/view/login";
        }
            return "/view/register";
    }

}
